package lib.modules;

import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;

import com.google.gdata.data.DateTime;

import flexjson.JSONSerializer;

import lib.base.EgoAuth;
import lib.base.EgoHoliday;
import lib.base.EgoModule;
import lib.base.EgoSystem;
import lib.data.Department;
import lib.translation.Translator;

public class SidebarGadget extends EgoModule
{
	private int _days;
	
	public SidebarGadget(HttpServletRequest req)
	{
		super(req);
		req.getSession().setAttribute("template", "modules/sidebar/frame.jsp");
		
		if (!_curCust.getField("temporarySystemAccount").toString().isEmpty())
		{
			// link for account move
			Calendar cal = Calendar.getInstance();
			DateTime d = DateTime.parseDate(_curCust.getField("createDate").toString());
			
			int free_days = 30;
			_days = free_days - (int)((cal.getTimeInMillis() - d.getValue())/1000) / (60*60*24);
			if (_days < 0) _days = 0;
		} else
		{
			_days = -1;
		}
	}

	@Override
	public void work() throws Exception
	{
		if (_request.getParameter("action") != null)
		{
			_read();
		} else
		{
			if (_curEmp.getField("firstLogin").toString() == "true")
			{
				_request.setAttribute("firstLogin", true);
				_curEmp.setField("firstLogin", "0");
				_curEmp.update();
				
				_request.setAttribute(
					"gadgeturl"
					, "http://www.google.com/calendar/render?cid="
						+ EgoSystem.config.get("host").toString()
						+ EgoSystem.config.get("path").toString()
						+ "gadgetEvents14.ics"
				);
			} else
			{
				if (_days != -1)
				{
					_request.setAttribute(
							"days"
							, _days
					);
				}
			}
		}
	}

	private void _read() throws Exception
	{
		ArrayList<String> menu = new ArrayList<String>();
		
		// time has expired
		if (_days != 0)
		{
			Translator t = EgoSystem.translator.get(_request.getSession().getAttribute("language").toString());
			
			// administrator
			if (((EgoAuth) _request.getSession().getAttribute("auth")).isAdmin())
			{
				menu.add(t.translate("Administration")+"|#|?mdl=admin");
			}
			
			if (_request.getSession().getAttribute("currentEmployee") != null)
			{
				/*
				 * we are logged in as an employee
				 */
				menu.add(t.translate("Leave Account")+"|#|?mdl=leave_account");
				
				
				// department supervisor
				ArrayList<Department> deps = _curEmp.getDepartments("supervisor");
				if (deps.size() > 0)
				{
					ArrayList<String> added_items = new ArrayList<String>();
	
					// for current and next year
					Calendar cal = Calendar.getInstance();
					int cur_year = cal.get(Calendar.YEAR);
					for (int iy=0; iy<2; iy++)
					{
						cur_year += iy;
	
						for (int i=0; i<deps.size(); i++)
						{
							ArrayList<EgoHoliday> holidays = _curCust.holidayManager.getHolidays(
								deps.get(i)
								, EgoHoliday.STATUS_REQUESTED
								, String.valueOf(cur_year)
							);
							
							// avoid duplicated entries
							for (int ii=0; ii<holidays.size(); ii++)
							{
								// cannot manage own leaves
								if (!_curEmp.getField("manageOwnLeaves").toString().equals("true") && holidays.get(ii).getEmployee().getField("employeeID").equals( _curEmp.getField("employeeID") ))
								{
									continue;
								}
								
								String name = holidays.get(ii).getEmployee().getField("account").toString() + "|";
								name += holidays.get(ii).getStartStamp();
								
								boolean added = false;
								for (int z=0; z<added_items.size(); z++)
								{
									if (added_items.get(z).equals( name ))
									{
										added = true;
									}
								}
								if (!added)
								{
									added_items.add(name);
								}
							}
						}
					}
					if (added_items.size()>0)
					{
						menu.add("<b>"+t.translate("Leave Requests")+" ("+ String.valueOf(added_items.size()) +")</b>|#|?mdl=leave_requests");
					} else
					{
						menu.add(t.translate("Leave Requests")+"|#|?mdl=leave_requests");
					}
				}
			}
			
			_request.setAttribute("ajax_return", new JSONSerializer().serialize(menu));
		}
		
		_request.getSession().setAttribute("template", "ajax_return.jsp");
	}
}
